//
//  MyGetDateView.m
//  EP
//
//  Created by yanfa on 15/8/24.
//  Copyright (c) 2015年 Sun. All rights reserved.
//

#import "MyGetDateView.h"

#define kDuration  0.3

@implementation MyGetDateView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (IBAction)pickValueChanged:(id)sender {
    
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone* sourceTimeZone = [NSTimeZone systemTimeZone];
    dateformatter.timeZone = sourceTimeZone;
    NSString* dateStr = [dateformatter stringFromDate:_datapick.date];
    _selectDate = dateStr;
}

-(id)initWithTitle:(NSString *)title delegate:(id)delegate{
    self = [[[NSBundle mainBundle] loadNibNamed:@"MyGetDateView" owner:self options:nil] objectAtIndex:0];
    self.delegate = delegate;
    _label_title.text = title;
//    _label_title.backgroundColor = UIColorFromRGB(MAINCOLOR);
    NSDate* date = [NSDate date];
    [_datapick setMaximumDate:date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    dateformatter.timeZone = [NSTimeZone systemTimeZone];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone* sourceTimeZone = [NSTimeZone systemTimeZone];
    dateformatter.timeZone = sourceTimeZone;
    NSString* dateStr = [dateformatter stringFromDate:date];
    _selectDate = dateStr;
    [_datapick setTimeZone:[NSTimeZone defaultTimeZone]];
    [_datapick setLocale:[NSLocale localeWithLocaleIdentifier:@"zh-CN"]];
    _datapick.datePickerMode=UIDatePickerModeDate;
    [_datapick setMinuteInterval:(24*60)];
    return self;
}

+(NSDate *)stringDateToDate:(NSString *)date{
    
    NSDate *resultDate;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone* sourceTimeZone = [NSTimeZone systemTimeZone];
    formatter.timeZone = sourceTimeZone;
    resultDate = [formatter dateFromString:[NSString stringWithFormat:@"%@",date]];
    return resultDate;
}


-(void)showInView:(UIView *)view{
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromTop;
    [self setAlpha:1.0f];
    [self.layer addAnimation:animation forKey:nil];
    self.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    [view addSubview:self];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self btnClick_Cancel:nil];
}

- (IBAction)btnClick_Cancel:(id)sender {
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:nil];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
    if(self.delegate) {
        [self.delegate actionSheet:self clickedButtonAtIndex:0];
    }
}

- (IBAction)btnClick_OK:(id)sender {
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:@"TSLocateView"];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
    if(self.delegate) {
        [self.delegate actionSheet:self clickedButtonAtIndex:1];
    }
}
@end
